<?php

namespace App\Http\Controllers;

use App\Club;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Storage;

class ClubController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $clubs = Club::latest('created_at')->Paginate(8);
        return view('Club.club', compact('clubs','coaches'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('Club.createClub');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rules = [
            'club_name' => 'required',
            'club_intro' => 'required|min:6',
            'club_pic' => 'required',
            'club_address' => 'required',
            'club_phone' => 'required',
            'work_time' => 'required'
        ];

        $this -> validate($request, $rules);
        $this -> uploadAvatar($request);
        $clubPic = $request -> get('club_name')
            . '.' . $request -> file('club_pic')
                -> getClientOriginalExtension();

        $club = new Club;
        $club -> club_pic = $clubPic;
        $club -> club_name = $request -> get('club_name');
        $club -> club_intro = $request -> get('club_intro');
        $club -> club_address = $request -> get('club_address');
        $club -> club_phone = $request -> get('club_phone');
        $club -> work_time = $request -> get('work_time');

        $club -> save();

        session() -> flash('success', '添加俱乐部信息成功');

        return redirect() -> route('club.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $club = Club::findOrFail($id);
        return view('Club.editClub', compact('club'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $club = Club::findOrFail($id);

        if ($request -> file('club_pic'))
        {
            $pic = $club -> club_pic;
            if ($pic != 'default.jpg')
            {
                Storage::disk('clubs_pic') -> exists($pic);
                Storage::disk('clubs_pic') -> delete($pic);
            }
            $this -> uploadAvatar($request);
            $club -> club_pic = $request -> get('club_name')
            . '.' . $request -> file('club_pic')
                -> getClientOriginalExtension();
            $club -> save();
        }

        $club -> update([
            'club_name' => $request -> get('club_name'),
            'club_intro' => $request -> get('club_intro'),
            'club_address' => $request -> get('club_address'),
            'club_phone' => $request -> get('club_phone'),
            'work_time' => $request -> get('work_time'),
        ]);
        $club -> save();

        session() -> flash('success', '更新成功');
        return redirect() -> route('club.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $club = Club::findOrFail($id);
        $pic = $club -> club_pic;
        if ($pic != 'default.jpg')
        {
            Storage::disk('clubs_pic') -> exists($pic);
            Storage::disk('clubs_pic') -> delete($pic);
        }
        $club -> delete();

        session() -> flash('success', '删除成功');
        return redirect() -> route('club.index');
    }

    /**
    * Upload Pic to public_path
    * @param Request $request
    */
    public function uploadAvatar(Request $request)
    {
        if ($request->isMethod('post') OR $request->isMethod('patch')) {

            $pic = $request->file('club_pic');

            if ($pic->isValid()) {

                $realPath = $pic->getRealPath();
                $ext = $pic->getClientOriginalExtension();
                $clubName = $request->get('club_name');
                $fileName = $request->get('club_name'). '.' .$ext;
                Image::make($pic)->fit(300, 300)->save(public_path('/uploads/clubs_pic/clubs/') . $fileName);
            }
        }
    }
}
